package com.ruoyi.common.utils.geotools;

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;

public class GeoCalculatorUtils {

    public static void main(String[] args) {
        /*double distance = getDistance(113.805695, 22.678611, 113.826391, 22.716473);
        System.out.println("获取两点之间的距离（米）|"+distance);*/


        System.out.println("根据输入的圆心坐标、另外一个点坐标、半径判断另外一个点是否在圆范围内|"+
                isItWithinTheScope(113.805695, 22.678611, 113.826391, 22.716473,4702));

        Coordinate[] coordinates = new Coordinate[]{
                new Coordinate(113.805695,22.678611),
                new Coordinate(113.815695,22.688611),
                new Coordinate(113.826371,22.698611),
                new Coordinate(113.826381,22.708611),
                new Coordinate(113.826391,22.716473),
                new Coordinate(113.805695,22.678611)
        };
        Coordinate coordinate = new Coordinate(113.815695,22.688611);
        System.out.println("根据输入的坐标集合、另外一个点坐标、半径判断另外一个点是否在坐标范围内|"+isItWithinTheScope2(coordinates,coordinate));
    }

    /**
     * 获取两点之间的距离（米）.
     *
     * @param longitude1 the longitude 1
     * @param latitude1  the latitude 1
     * @param longitude2 the longitude 2
     * @param latitude2  the latitude 2
     * @return the distance
     * @author :yanlixiong
     * @date :2024-11-15 19:41:56
     */
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 84坐标系构造GeodeticCalculator
        GeodeticCalculator geodeticCalculator = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
        // 起点经纬度
        geodeticCalculator.setStartingGeographicPoint(longitude1, latitude1);
        // 末点经纬度
        geodeticCalculator.setDestinationGeographicPoint(longitude2, latitude2);
        // 计算距离，单位：米
        return geodeticCalculator.getOrthodromicDistance();
    }

    /**
     * 根据输入的圆心坐标、另外一个点坐标、半径判断另外一个点是否在圆范围内.
     *
     * @param longitude1 the longitude 1
     * @param latitude1  the latitude 1
     * @param longitude2 the longitude 2
     * @param latitude2  the latitude 2
     * @param radius     the radius
     * @return the boolean
     * @author :yanlixiong
     * @date :2024-11-15 19:43:22
     */
    public static boolean isItWithinTheScope(double longitude1, double latitude1, double longitude2, double latitude2,double radius) {
        double distance = getDistance(longitude1,latitude1,longitude2,latitude2);
        return distance<radius;
    }

    /**
     * 根据输入的坐标集合、另外一个点坐标、半径判断另外一个点是否在坐标范围内.
     *
     * @param coordinates the coordinates
     * @param coordinate  the coordinate
     * @return the boolean
     * @author :yanlixiong
     * @date :2024-11-15 19:53:06
     */
    public static boolean isItWithinTheScope2(Coordinate[] coordinates, Coordinate coordinate) {
        // 创建几何工厂
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        // 创建多边形
        Polygon polygon = geometryFactory.createPolygon(coordinates);
        // 定义一个点
        Point point = geometryFactory.createPoint(coordinate);
        // 调用方法检查点是否在多边形内部
        return polygon.contains(point);
    }

}
